package com.jinlejia.erp.ui.new_house.adapter;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;

import java.util.ArrayList;
import java.util.Map;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;



/**
 * ViewPagerAdapter
 */

public class PictureViewPager extends PagerAdapter {

    private Context context;
    private ArrayList<String> list;

    public PictureViewPager(Context context , ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_house_picture,null);
        PhotoView imageView = (PhotoView) view.findViewById(R.id.house_type_image);

        if (!TextUtils.isEmpty(list.get(position).toString())){
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL().concat(list.get(position).toString()))
            .placeholder(R.mipmap.ic_no_img).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        }
        imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float x, float y) {
                listen.OnItemClick(position);
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public interface OnItemClickListen{
        void OnItemClick(int position);
    }
    private OnItemClickListen listen;

    public void OnItemClick(OnItemClickListen listen){
        this.listen = listen;
    }
}
